package com.mi.android;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = MyReceiver.class.getName();
	private java.util.Calendar calendar = java.util.Calendar.getInstance();

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction());
		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			Log.i(TAG, "[MyReceiver] 接收Registration Id : " + regId);
			// send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			processCustomMessage(context, bundle);
		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 接收到推送下来的通知");
			int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			Log.i(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 用户点击打开了通知");
			Center.getInstance().launchMiShop(context);
			// //打开自定义的Activity
			// Intent i = new Intent(context, TestActivity.class);
			// i.putExtras(bundle);
			// //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
			// Intent.FLAG_ACTIVITY_CLEAR_TOP );
			// context.startActivity(i);

		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
		} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
			boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
			if (!connected) {
				JPushInterface.resumePush(context);
			}
		} else {
			Log.i(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
		}
	}

	public PendingIntent getDefalutIntent(android.content.Context context) {
		Intent intent = new Intent();
		intent.setClass(context, MyReceiver.class);
		intent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		return pendingIntent;
	}

	private void processCustomMessage(Context context, Bundle bundle) {
		android.net.Uri uri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM);
		String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
		Log.d(TAG, "消息内容：" + message);// 消息内容
		Log.d(TAG, "扩展字段：" + extras);// 扩展字段，JSON格式
		Log.d(TAG, "通知编号：" + notificationId);// 扩展字段，JSON格式
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Builder mBuilder = new Builder(context);
		mBuilder.setContentTitle(message);
		mBuilder.setContentIntent(getDefalutIntent(context));
		mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
		mBuilder.setAutoCancel(true);
		mBuilder.setSound(uri);
		mBuilder.setSmallIcon(R.drawable.ic_launcher);
		try {
			if (extras != null && !extras.equals("")) {
				JSONObject json = new JSONObject(extras);
				long time = json.optLong("time", 0L);
				String color = json.optString("color");
				if (color != null && !color.equals("")) {
					calendar.setTimeInMillis(time);
					mBuilder.setContentText(color + "（" + calendar.get(java.util.Calendar.HOUR_OF_DAY) + ":"
							+ calendar.get(java.util.Calendar.MINUTE) + ":" + calendar.get(java.util.Calendar.SECOND)
							+ "）");
				}
				mBuilder.setWhen(time);
			}
		} catch (JSONException e) {
			Log.w(TAG, "json格式错误", e);
		} finally {
			Log.d(TAG, "发送通知栏");
			mNotificationManager.notify(notificationId, mBuilder.build());
		}
	}
}
